package cn.pzhu.services.web;

import cn.pzhu.exception.MyException;
import cn.pzhu.model.Achievement;
import cn.pzhu.model.Demand;
import cn.pzhu.model.PageBean;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by dongp on 2016/5/8.
 */
public class WebAcheievementServices {

    /**
     * 默认查询前6条数据
     * @return
     */
    public List querySix() {
        List<Achievement> achievements = Achievement.dao.find("select * from issueacheievement t1 where t1.status=3 limit " + 0 + "," + 6);
        return achievements;
    }

    public List queryPage(HttpServletRequest request, String typeVal, String search) {
        // session获取分页参数
        HttpSession session = request.getSession();
        PageBean pageBean = (PageBean) session.getAttribute("pageBean");
        if(pageBean == null) {
            pageBean = new PageBean();
        }
        String page = request.getParameter("page");
        if(page != null && page != "") {
            pageBean.setPageOffset(Integer.parseInt(page));
        }

        List<Achievement> achievements = Achievement.dao.find("select t1.*,t2.typeName,t2.typeVal from issueacheievement t1 "+
                "left join category t2 on t1.categoryVal=t2.typeVal where t1.status=3 and t1.categoryVal like '%"+typeVal+"%' and t1.name like '%"+
                search+"%' limit "+ (pageBean.getPageOffset()-1)*pageBean.getPageSize()+
                ","+pageBean.getPageSize());

        Achievement achievement = Achievement.dao.findFirst("select count(*) total from issueacheievement t1 where " +
                " t1.status=3 and t1.categoryVal like '%"+typeVal+"%' and t1.name like '%"+search+"%'");
        int total = achievement.getLong("total").intValue();
        pageBean.setTotal(total);
        session.setAttribute("pageBean", pageBean);
        return achievements;
    }

    public Achievement queryDetail(int id) {
        Achievement achievement = Achievement.dao.findFirst("select t1.*,t2.* from issueacheievement t1 left join " +
                " category t2 on t1.categoryVal=t2.typeVal where t1.status=3 and t1.acheievementID = ? ",id);

        return achievement;
    }

    public List queryByUserId(int userId) {
        List<Achievement> achievements = Achievement.dao.find("select t1.*,t2.* from issueacheievement t1 left join " +
                " category t2 on t1.categoryVal=t2.typeVal where t1.userID = ? ", userId);

        return achievements;
    }

    public boolean addAchievement(int userID, int categoryID, String name, String level, String imgeurl, String isPat,
                          String desc, String tradeMode, String serverPromise, String provideInfo, String phone,
                          String address, double money, String rank, String tel,double rebate) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String date = format.format(new Date());

        boolean result = new Achievement().set("userID",userID).set("categoryVal",categoryID).set("name",name).set("level",level)
                .set("imgeurl", imgeurl).set("isPat",isPat).set("desc",desc).set("tradeMode",tradeMode).set("serverPromise",
                serverPromise).set("provideInfo",provideInfo).set("phone",phone).set("address",address).set("money",money)
                .set("rank",rank).set("tel",tel).set("rebate",rebate).set("status",4).set("issueDate",date).save();
        return result;
    }

    public boolean modifyAchievement(int id, int userID, int categoryID, String name, String level, String isPat,
                                  String desc, String tradeMode, String serverPromise, String provideInfo, String phone,
                                  String address, double money, String rank, String tel,double rebate) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String date = format.format(new Date());

        boolean result = Achievement.dao.findById(id).set("userID",userID).set("categoryVal",categoryID).set("name",name).set("level",level)
                .set("isPat",isPat).set("desc",desc).set("tradeMode",tradeMode).set("serverPromise",
                serverPromise).set("provideInfo",provideInfo).set("phone",phone).set("address",address).set("money",money)
                .set("rank",rank).set("tel",tel).set("rebate",rebate).set("status",4).set("issueDate",date).update();
        return result;
    }

    public boolean addDemand(int userID, int categoryID, String name, double budget, String demandDesc, String organization,
                                  String phone, String address) {

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String date = format.format(new Date());

        boolean result = new Demand().set("userID", userID).set("categoryID", categoryID).set("name",name).set("budget", budget)
                .set("demandDesc", demandDesc).set("organization", organization).set("phone",phone).set("address", address)
                .set("status", 4).set("issueDate",date).save();
        return result;
    }

    public boolean delAchievement(int id) {
        boolean result = Achievement.dao.deleteById(id);
        return result;
    }

}
